package com.qingcheng.controller;

import com.qingcheng.domain.Goods;
import com.qingcheng.entity.PageResult;
import com.qingcheng.entity.Result;
import com.qingcheng.pojo.goods.Spu;
import com.qingcheng.pojo.goods.TbSpu;
import com.qingcheng.service.SpuService;
import com.qingcheng.service.TbSpuService;
import com.qingcheng.util.PageUtil;
import com.qingcheng.util.YouthR;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 *
 * @author Simon
 * @date 2020/07/27
 * 商品
 */
@Slf4j
@RestController
@RequestMapping("/spu")
@Api(tags = "TbSpuController")
public class SpuController {

    @Autowired
    private SpuService spuService;

    @Autowired
    private TbSpuService tbspuService;

    @Resource
    private TbSpuService tbSpuService;

    @ApiOperation(value = "商品审核de数据回显")
    @PostMapping("/recycleBin/{pageNum}/{pageSize}")
    public  YouthR recycleBin(@PathVariable Integer pageNum,@PathVariable Integer pageSize,@RequestBody TbSpu spu){
        PageUtil<TbSpu> tbSpuPageUtil = tbSpuService.CommodityRecyclingStation(pageNum, pageSize, spu);
        return YouthR.ok().data("pageUtils",tbSpuPageUtil);
    }

    @ApiOperation(value = "物理删除")
    @DeleteMapping("/deleteByIdSpu/{id}")
    public YouthR deleteByIdSpu(@PathVariable String id){
        boolean result = tbSpuService.removeSpuIsDelete(id);
        if(result)
            return YouthR.ok();
        return YouthR.error();
    }

    @ApiOperation(value = "逻辑删除商品，修改spu表is_delete字段为1，回收商品，修改spu表is_delete字段为0")
    @DeleteMapping("/putMany/{id}")
    public YouthR IsDelete(@PathVariable String id){
        boolean result = tbSpuService.removeSpuIsDelete(id);
        if(result)
            return YouthR.ok();
        return YouthR.error();
    }

    @ApiOperation(value = "前端传递一组商品ID，后端进行批量上下架处理，处理后给前端返回处理的条数\n")
    @PutMapping("/putMany/{ids}")
    public YouthR putMany(@PathVariable Long[] ids){
        int count = tbSpuService.putMany(ids);
        return YouthR.ok().data("message","上架了"+count+"商品");
    }

    @ApiOperation(value = "上架商品")
    @PutMapping("/put")
    public YouthR put(String id){
        boolean put = tbSpuService.put(id);
        if (put)
            return YouthR.ok();
        return YouthR.error();
    }

    @ApiOperation(value = "下架商品")
    @PutMapping("/pull/{id}")
    public YouthR pull(@PathVariable String id){
        boolean pull = tbSpuService.pull(id);
        if (pull)
            return YouthR.ok();
        return YouthR.error();
    }

    @ApiOperation(value = "商品审核")
    @PutMapping("/audit")
    public YouthR audit(@RequestBody TbSpu tbSpu){
        boolean audit = tbSpuService.audit(tbSpu);
        if (audit)
            return YouthR.ok();
        return YouthR.error();
    }

    @ApiOperation(value = "商品审核de数据回显")
    @PostMapping("/queryAudit/{pageNum}/{pageSize}")
    public  YouthR queryAudit(@PathVariable Integer pageNum,@PathVariable Integer pageSize,@RequestBody TbSpu spu){
        PageUtil<TbSpu> tbSpuPageUtil = tbSpuService.queryAudit(pageNum, pageSize, spu);
        return YouthR.ok().data("pageUtils",tbSpuPageUtil);
    }

    @ApiOperation(value = "根据id获取信息")
    @GetMapping("/detail/{id}")
    public YouthR detail(@PathVariable String id) {
        Goods goods = tbSpuService.findGoodsById(id);
        return YouthR.ok().data("goods",goods);
    }

    @ApiOperation(value = "获取信息（list不分页）")
    @GetMapping("/list")
    public YouthR list() {
        List<TbSpu>  list = tbSpuService.queryAll();
        return YouthR.ok().data("list",list);
    }



    @ApiOperation(value = "获取信息（list分页）不带条件")
    @GetMapping("/listPages")
    public YouthR listPages(Integer pageNum,Integer pageSize) {
        PageUtil<TbSpu> pageUtils = tbSpuService.findListAll(pageNum, pageSize);

        return YouthR.ok().data("pageUtils",pageUtils);
    }

    @ApiOperation(value = "获取信息（list分页）带条件")
    @PostMapping("pageQueryByAll/{pageNum}/{pageSize}")
    public YouthR pageQueryByAll(@PathVariable Integer pageNum,
                                 @PathVariable Integer pageSize,
                                 @RequestBody TbSpu tbSpu){
        PageUtil<TbSpu> pageUtils = tbSpuService.pageQueryByAll(pageNum, pageSize, tbSpu);
        return YouthR.ok().data("pageUtils",pageUtils);
    }

    @ApiOperation(value = "获取信息（list分页）带条件")
    @PostMapping("adQueryList/{pageNum}/{pageSize}")
    public YouthR adQueryList(@PathVariable Integer pageNum,
                                 @PathVariable Integer pageSize,
                                 @RequestBody TbSpu tbSpu){
        PageUtil<TbSpu> pageUtils = tbSpuService.adQueryAll(pageNum, pageSize, tbSpu);
        return YouthR.ok().data("pageUtils",pageUtils);
    }


    @ApiOperation(value = "修改信息")
    @PutMapping("/update")
    public YouthR update(@RequestBody TbSpu tbSpu) {
        boolean b = tbSpuService.updateTbSpu(tbSpu);
        if(b){
            return YouthR.ok();
        }
        return YouthR.error();
    }

    @ApiOperation(value = "添加信息")
    @PostMapping("/add")
    public YouthR add(@RequestBody Goods goods) {
        boolean b = tbSpuService.saveGoods(goods);
        if(b){
            return YouthR.ok();
        }
        return YouthR.error();
    }

    @ApiOperation(value = "删除信息（主键）")
    @DeleteMapping("/delete/{id}")
    public YouthR delete(@PathVariable Integer id) {
        boolean b = tbSpuService.deleteById(id);
        if(b){
            return YouthR.ok();
        }
        return YouthR.error();
    }

    @GetMapping("/findAll")
    public List<Spu> findAll(){
        return spuService.findAll();
    }

    @GetMapping("/findPage")
    public PageResult<Spu> findPage(int page, int size){
        return spuService.findPage(page, size);
    }

    @PostMapping("/findList")
    public List<Spu> findList(@RequestBody Map<String,Object> searchMap){
        return spuService.findList(searchMap);
    }

    @PostMapping("/findPage")
    public PageResult<Spu> findPage(@RequestBody Map<String,Object> searchMap,int page, int size){
        return  spuService.findPage(searchMap,page,size);
    }

    @GetMapping("/findById/{id}")
    public YouthR findById(@PathVariable String id){
        TbSpu byId = tbSpuService.findById(id);
        return YouthR.ok().data("spu",byId);
    }

    @ApiOperation("广告提交之后修改广告的状态")
    @PostMapping("/adUpdate/{id}")
    public YouthR adUpdate(@PathVariable String id){
        tbSpuService.updateAdStatus(id);
        return YouthR.ok();
    }


//    @PostMapping("/add")
//    public Result add(@RequestBody Spu spu){
//        spuService.add(spu);
//        return new Result(200,"添加成功",null);
//    }

    @GetMapping("/delete")
    public Result delete(String id){
        spuService.delete(id);
        return new Result(200,"删除成功",null);
    }

    @GetMapping("/findGoodById")
    public com.qingcheng.pojo.goods.Goods findGoodById(String id){
        return spuService.findGoodsById(id);
    }

    // 首页菜单的热卖商品显示
    @GetMapping("/best")
    public List<Map> Best(){
        return tbspuService.bestgoods();
    }

    // 显示菜单首页新品显示
    @GetMapping("/newproduct")
    public List<Map> newproduct(){
        return tbspuService.newproduct();
    }


    // 首页菜单的喜欢推荐
    @GetMapping("/like")
    List<Map> like(){ return tbspuService.like(); }
}
